/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on 18/07/2011, 19:09:39
 */
package fallen.deck.front_end;

import fallen.deck.Drecker;
import fallen.deck.entity.*;
import fallen.deck.front_end.jCustomTabbed.CustomDeckPanel;
import fallen.deck.front_end.jCustomTabbed.CustomTab;
import fallen.deck.hibernate.Queries;
import java.awt.HeadlessException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class MainWindow extends javax.swing.JFrame {

    private Deck actualDeck;
    
    /** Creates new form MainWindow */
    public MainWindow() {
        this.setIconImage(new ImageIcon(getClass().getResource("/icons/64/Deck-Creator-Icon.png")).getImage());
        initComponents();
        loadDecks();
        Drecker.inform("Decks loaded.");
        actualDeck = null;
    }

    public void showDeckOnText() {
        CustomDeckPanel tab = (CustomDeckPanel) jTabbedPane.getComponentAt(
                        jTabbedPane.getSelectedIndex());
        new ShareDeckWindow(tab.getDeck()).setVisible(true);
    }

    public void addDeckTab(Deck newDeck) {
        newDeck = Queries.getDeck(newDeck.getId());
        jTabbedPane.addTab("", new CustomDeckPanel(newDeck));
        //Trasladamos el enfoque a esta nueva ficha
        jTabbedPane.setSelectedIndex(jTabbedPane.getTabCount() - 1);
        //Creamos nuestra pestaña personalizada
        CustomTab tab = new CustomTab(jTabbedPane);
        //Agregamos la pestaña creada a nuestra ficha
        jTabbedPane.setTabComponentAt(jTabbedPane.getTabCount() - 1, tab);
        //Actualizamos la ventana
        deckSelectedupdate();
        loadDecks();
    }

    private void deckSelectedupdate() {
        try {
            actualDeck = ((CustomDeckPanel) jTabbedPane.getComponentAt(
                    jTabbedPane.getSelectedIndex())).getDeck();
            this.setTitle(actualDeck.getName()
                    + " - Creador de Mazos de INFERNO");
            loadDemonCombo(actualDeck.getDemon().getPathway(),
                    actualDeck.getDemon());
            enableActions();
            Drecker.inform(actualDeck.getName() + " selected.");
        } catch (NullPointerException e) {
            this.setTitle("Creador de Mazos de INFERNO");
            actualDeck = null;
            disableActions();
        } catch (ClassCastException e) {
            actualDeck = null;
            disableActions();
            Drecker.inform("No Deck tab selected.", Drecker.WARNING_MESSAGE);
        } catch (Exception e) {
            actualDeck = null;
            e.printStackTrace();
        }
    }

    private void enableActions() {
        //Toolbar
        //btnPrint.setEnabled(true);
        btnSave.setEnabled(true);
        btnSaveAll.setEnabled(true);
        btnSimulate.setEnabled(true);
        btnToText.setEnabled(true);
        btnStadistics.setEnabled(true);
        cboDemon.setEnabled(true);
        //Menu
        mnuF_Export.setEnabled(true);
        mnuF_Save.setEnabled(true);
    }

    private void disableActions() {
        //Toolbar
        btnPrint.setEnabled(false);
        btnSave.setEnabled(false);
        btnSaveAll.setEnabled(false);
        btnSimulate.setEnabled(false);
        btnToText.setEnabled(false);
        btnStadistics.setEnabled(false);
        cboDemon.removeAllItems();
        cboDemon.addItem("[No hay Selección]");
        cboDemon.setEnabled(false);
        //Menu
        mnuF_Export.setEnabled(false);
        mnuF_Save.setEnabled(false);
    }

    private void importDeck() throws HeadlessException {
        try {
            Drecker.importDeck();
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException e)
        {
            Drecker.inform(e.getMessage(), Drecker.ERROR_MESSAGE);
            Drecker.inform("Importación Cancelada.");
            JOptionPane.showMessageDialog(this, "El archivo .dkr no pudo ser importado.\nEs posible que esté dañado.",
                    "Importación Cancelada", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void launchNewDeckWizard() {
        new Wizard().setVisible(true);
    }

    private void loadDecks() {
        List decks = Queries.getDecks();
        lstDecks.removeAll();
        DefaultListModel listModel = new DefaultListModel();
        for (int i = 0; decks.size() > i; i++) {
            listModel.addElement(decks.get(i));
        }
        lstDecks.setModel(listModel);
    }

    private void loadDemonCombo(Pathway pathway, Card demon) {
        List demons = Queries.getDemons(pathway.getName());
        cboDemon.removeAllItems();
        int selected = -1;
        for (int i = 0; demons.size() > i; i++) {
            cboDemon.addItem(demons.get(i));
            if (((Card) demons.get(i)).compareTo(demon) == 0) {
                selected = i;
            }
        }
        cboDemon.setSelectedIndex(selected);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar = new javax.swing.JToolBar();
        lblDemonio = new javax.swing.JLabel();
        cboDemon = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnStadistics = new javax.swing.JButton();
        btnSimulate = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnSave = new javax.swing.JButton();
        btnSaveAll = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnPrint = new javax.swing.JButton();
        btnToText = new javax.swing.JButton();
        jTabbedPane = new javax.swing.JTabbedPane();
        welcomePanel = new javax.swing.JLayeredPane();
        btnNewDeck = new javax.swing.JButton();
        btnOpenDeck = new javax.swing.JButton();
        lblBienvenido = new javax.swing.JLabel();
        lblComenzar = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstDecks = new javax.swing.JList();
        btnDelDeck = new javax.swing.JButton();
        lblBackground = new javax.swing.JLabel();
        statusBarPanel = new javax.swing.JPanel();
        lblStatus = new javax.swing.JLabel();
        mainjProgressBar = new javax.swing.JProgressBar();
        jMenuBar = new javax.swing.JMenuBar();
        mnuFile = new javax.swing.JMenu();
        mnuF_New = new javax.swing.JMenuItem();
        mnuF_Import = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JPopupMenu.Separator();
        mnuF_Save = new javax.swing.JMenuItem();
        mnuF_SaveAll = new javax.swing.JMenuItem();
        mnuF_Export = new javax.swing.JMenuItem();
        jSeparator7 = new javax.swing.JPopupMenu.Separator();
        mnuF_Print = new javax.swing.JMenuItem();
        jSeparator8 = new javax.swing.JPopupMenu.Separator();
        mnuF_Exit = new javax.swing.JMenuItem();
        mnuEdit = new javax.swing.JMenu();
        mnuHelp = new javax.swing.JMenu();
        mnuH_About = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        mnuH_Help = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Creador de Mazos de INFERNO");

        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);
        jToolBar.setEnabled(false);

        lblDemonio.setText("Demonio: ");
        jToolBar.add(lblDemonio);

        cboDemon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboDemon.setMaximumSize(new java.awt.Dimension(200, 22));
        cboDemon.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                cboDemonPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });
        jToolBar.add(cboDemon);
        jToolBar.add(jSeparator1);

        btnStadistics.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/chart_bar.png"))); // NOI18N
        btnStadistics.setMnemonic('g');
        btnStadistics.setToolTipText("Mira las estadisticas y graficos sobre tu mazo");
        btnStadistics.setFocusable(false);
        btnStadistics.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnStadistics.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnStadistics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStadisticsActionPerformed(evt);
            }
        });
        jToolBar.add(btnStadistics);

        btnSimulate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/eye.png"))); // NOI18N
        btnSimulate.setMnemonic('m');
        btnSimulate.setToolTipText("Simula una mano inicial");
        btnSimulate.setFocusable(false);
        btnSimulate.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSimulate.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSimulate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimulateActionPerformed(evt);
            }
        });
        jToolBar.add(btnSimulate);
        jToolBar.add(jSeparator3);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/disk.png"))); // NOI18N
        btnSave.setMnemonic('s');
        btnSave.setToolTipText("Guarda el mazo actual");
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar.add(btnSave);

        btnSaveAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/disk_multiple.png"))); // NOI18N
        btnSaveAll.setToolTipText("Guarda todos los mazos");
        btnSaveAll.setFocusable(false);
        btnSaveAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSaveAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSaveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveAllActionPerformed(evt);
            }
        });
        jToolBar.add(btnSaveAll);
        jToolBar.add(jSeparator4);

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/printer.png"))); // NOI18N
        btnPrint.setMnemonic('p');
        btnPrint.setToolTipText("Imprime tu mazo");
        btnPrint.setFocusable(false);
        btnPrint.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPrint.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnPrint);

        btnToText.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/page_white_code.png"))); // NOI18N
        btnToText.setToolTipText("Listado de cartas a texto");
        btnToText.setFocusable(false);
        btnToText.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToText.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToTextActionPerformed(evt);
            }
        });
        jToolBar.add(btnToText);

        jTabbedPane.setDoubleBuffered(true);
        jTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPaneStateChanged(evt);
            }
        });
        jTabbedPane.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                jTabbedPaneComponentAdded(evt);
            }
        });

        btnNewDeck.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/32/New_Deck_32.png"))); // NOI18N
        btnNewDeck.setMnemonic('n');
        btnNewDeck.setToolTipText("Nuevo Mazo");
        btnNewDeck.setFocusable(false);
        btnNewDeck.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNewDeck.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNewDeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewDeckActionPerformed(evt);
            }
        });
        btnNewDeck.setBounds(10, 280, 110, 42);
        welcomePanel.add(btnNewDeck, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnOpenDeck.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/32/Open_Deck_32.png"))); // NOI18N
        btnOpenDeck.setToolTipText("Importar un Mazo");
        btnOpenDeck.setFocusable(false);
        btnOpenDeck.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnOpenDeck.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnOpenDeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenDeckActionPerformed(evt);
            }
        });
        btnOpenDeck.setBounds(10, 330, 230, 42);
        welcomePanel.add(btnOpenDeck, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblBienvenido.setForeground(java.awt.Color.white);
        lblBienvenido.setText("Bienvenido a Drecker, el Creador de Mazo.");
        lblBienvenido.setBounds(10, 10, 380, 20);
        welcomePanel.add(lblBienvenido, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblComenzar.setForeground(java.awt.Color.white);
        lblComenzar.setText("Puedes comenzar cuando estés listo haciendo click en alguna de las dos opciones de abajo.");
        lblComenzar.setBounds(10, 40, 910, 20);
        welcomePanel.add(lblComenzar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lstDecks.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstDecks.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstDecks.setToolTipText("Doble Click para abrir el mazo");
        lstDecks.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstDecksMouseClicked(evt);
            }
        });
        lstDecks.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstDecksValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstDecks);

        jScrollPane1.setBounds(10, 70, 230, 200);
        welcomePanel.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnDelDeck.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/32/Del_Deck_32.png"))); // NOI18N
        btnDelDeck.setToolTipText("Borrar Mazo");
        btnDelDeck.setEnabled(false);
        btnDelDeck.setFocusable(false);
        btnDelDeck.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDelDeck.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDelDeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelDeckActionPerformed(evt);
            }
        });
        btnDelDeck.setBounds(130, 280, 110, 42);
        welcomePanel.add(btnDelDeck, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/big_images/background.jpg"))); // NOI18N
        lblBackground.setBounds(0, 0, 1600, 1200);
        welcomePanel.add(lblBackground, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane.addTab("Bienvenido!", welcomePanel);

        statusBarPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblStatus.setToolTipText("Estado Actual");

        mainjProgressBar.setToolTipText("Progreso");

        javax.swing.GroupLayout statusBarPanelLayout = new javax.swing.GroupLayout(statusBarPanel);
        statusBarPanel.setLayout(statusBarPanelLayout);
        statusBarPanelLayout.setHorizontalGroup(
            statusBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusBarPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 755, Short.MAX_VALUE)
                .addComponent(mainjProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        statusBarPanelLayout.setVerticalGroup(
            statusBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblStatus)
            .addComponent(mainjProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        mnuFile.setText("Archivo");

        mnuF_New.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnuF_New.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/new_deck.png"))); // NOI18N
        mnuF_New.setText("Nuevo Mazo");
        mnuF_New.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuF_NewActionPerformed(evt);
            }
        });
        mnuFile.add(mnuF_New);

        mnuF_Import.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        mnuF_Import.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/open_deck.png"))); // NOI18N
        mnuF_Import.setText("Importar Mazo");
        mnuF_Import.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuF_ImportActionPerformed(evt);
            }
        });
        mnuFile.add(mnuF_Import);
        mnuFile.add(jSeparator6);

        mnuF_Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mnuF_Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/disk.png"))); // NOI18N
        mnuF_Save.setText("Guardar");
        mnuF_Save.setEnabled(false);
        mnuF_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuF_SaveActionPerformed(evt);
            }
        });
        mnuFile.add(mnuF_Save);

        mnuF_SaveAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/disk_multiple.png"))); // NOI18N
        mnuF_SaveAll.setText("Guardar Todo");
        mnuF_SaveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuF_SaveAllActionPerformed(evt);
            }
        });
        mnuFile.add(mnuF_SaveAll);

        mnuF_Export.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/drive_disk.png"))); // NOI18N
        mnuF_Export.setText("Exportar Mazo");
        mnuF_Export.setEnabled(false);
        mnuF_Export.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuF_ExportActionPerformed(evt);
            }
        });
        mnuFile.add(mnuF_Export);
        mnuFile.add(jSeparator7);

        mnuF_Print.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        mnuF_Print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/printer.png"))); // NOI18N
        mnuF_Print.setText("Imprimir");
        mnuF_Print.setEnabled(false);
        mnuFile.add(mnuF_Print);
        mnuFile.add(jSeparator8);

        mnuF_Exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        mnuF_Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/door_in.png"))); // NOI18N
        mnuF_Exit.setText("Salir");
        mnuF_Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuF_ExitActionPerformed(evt);
            }
        });
        mnuFile.add(mnuF_Exit);

        jMenuBar.add(mnuFile);

        mnuEdit.setText("Editar");
        jMenuBar.add(mnuEdit);

        mnuHelp.setText("Ayuda");

        mnuH_About.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/information.png"))); // NOI18N
        mnuH_About.setText("Acerca de...");
        mnuH_About.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuH_AboutActionPerformed(evt);
            }
        });
        mnuHelp.add(mnuH_About);
        mnuHelp.add(jSeparator5);

        mnuH_Help.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/16/help.png"))); // NOI18N
        mnuH_Help.setText("Como comenzar...");
        mnuHelp.add(mnuH_Help);

        jMenuBar.add(mnuHelp);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 933, Short.MAX_VALUE)
            .addComponent(statusBarPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 933, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusBarPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-935)/2, (screenSize.height-559)/2, 935, 559);
    }// </editor-fold>//GEN-END:initComponents

    private void btnNewDeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewDeckActionPerformed
        launchNewDeckWizard();

    }//GEN-LAST:event_btnNewDeckActionPerformed

private void mnuH_AboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuH_AboutActionPerformed
    new AboutWindow().setVisible(true);
}//GEN-LAST:event_mnuH_AboutActionPerformed

private void btnSimulateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimulateActionPerformed
    generateRandomHandActualDeck();
}//GEN-LAST:event_btnSimulateActionPerformed

private void jTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPaneStateChanged
    deckSelectedupdate();
}//GEN-LAST:event_jTabbedPaneStateChanged

private void jTabbedPaneComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_jTabbedPaneComponentAdded
}//GEN-LAST:event_jTabbedPaneComponentAdded

private void mnuF_NewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuF_NewActionPerformed
    launchNewDeckWizard();
}//GEN-LAST:event_mnuF_NewActionPerformed

private void btnDelDeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelDeckActionPerformed
    removeSelectedDeck();
}//GEN-LAST:event_btnDelDeckActionPerformed

private void lstDecksValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstDecksValueChanged
    btnDelDeck.setEnabled(lstDecks.getSelectedIndex() >= 0);
}//GEN-LAST:event_lstDecksValueChanged

private void lstDecksMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstDecksMouseClicked
    if (evt.getClickCount() == 2) {
        Deck deck = (Deck) lstDecks.getModel().getElementAt(lstDecks.getSelectedIndex());
        for (int i = 1; i < jTabbedPane.getTabCount(); i++) {
            if (deck.compareTo(((CustomDeckPanel) jTabbedPane.getComponentAt(i)).getDeck()) == 0) {
                jTabbedPane.setSelectedIndex(i);
                Drecker.inform(deck.getName() + " already opened.");
                return;
            }
        }
        Drecker.addDeckTab(deck);
    }
}//GEN-LAST:event_lstDecksMouseClicked

private void cboDemonPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cboDemonPopupMenuWillBecomeInvisible
    if (cboDemon.getSelectedItem() != null) {
        actualDeck.setDemon((Card) cboDemon.getSelectedItem());
    }
}//GEN-LAST:event_cboDemonPopupMenuWillBecomeInvisible

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    saveActualDeck();
}//GEN-LAST:event_btnSaveActionPerformed

private void mnuF_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuF_SaveActionPerformed
    saveActualDeck();
}//GEN-LAST:event_mnuF_SaveActionPerformed

private void btnSaveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveAllActionPerformed
        saveAllDecks();
}//GEN-LAST:event_btnSaveAllActionPerformed

private void mnuF_SaveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuF_SaveAllActionPerformed
    saveAllDecks();
}//GEN-LAST:event_mnuF_SaveAllActionPerformed

private void mnuF_ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuF_ExitActionPerformed
    this.dispose();
}//GEN-LAST:event_mnuF_ExitActionPerformed

private void mnuF_ExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuF_ExportActionPerformed
    Drecker.exportDeck(actualDeck);
}//GEN-LAST:event_mnuF_ExportActionPerformed

private void mnuF_ImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuF_ImportActionPerformed
        importDeck();
}//GEN-LAST:event_mnuF_ImportActionPerformed

private void btnOpenDeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenDeckActionPerformed
        importDeck();
}//GEN-LAST:event_btnOpenDeckActionPerformed

private void btnToTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToTextActionPerformed
        showDeckOnText();
}//GEN-LAST:event_btnToTextActionPerformed

    private void btnStadisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStadisticsActionPerformed
        CustomDeckPanel tab = (CustomDeckPanel) jTabbedPane.getComponentAt(
                jTabbedPane.getSelectedIndex());
        new StadisticsWindow(tab.getDeckMetadata()).setVisible(true);
    }//GEN-LAST:event_btnStadisticsActionPerformed

    private void saveAllDecks() {
        for (int i = 1; i < jTabbedPane.getTabCount(); i++) {
            CustomDeckPanel tab = (CustomDeckPanel) jTabbedPane.getComponentAt(
                    jTabbedPane.getSelectedIndex());
            Drecker.inform("Saving deck " + tab.getDeck().getName());
            tab.updateDeckNotes();
            saveDeck(tab.getDeck());
        }
    }

    private void saveDeck(Deck deck) {
        String saveResult;
        if (Queries.save(deck))
            saveResult = "Mazo "+deck.getName()+" guardado";
        else
            saveResult = "No se pudo guardar el mazo " + deck.getName();
        Drecker.inform(saveResult);
    }

    private void saveActualDeck() {
        CustomDeckPanel tab = (CustomDeckPanel) jTabbedPane.getComponentAt(
                jTabbedPane.getSelectedIndex());
        Drecker.inform("Saving deck-" + tab.getDeck().getId());
        tab.updateDeckNotes();
        saveDeck(tab.getDeck());
    }
    
    private void generateRandomHandActualDeck() {
        CustomDeckPanel tab = (CustomDeckPanel) jTabbedPane.getComponentAt(
                jTabbedPane.getSelectedIndex());
        if (tab.getDeck().getCards().size() < 40){
            JOptionPane.showMessageDialog(this, "El mazo debe tener 40 cartas como mínimo para generar una mano simulada."
                    , "Cartas Insuficientes", JOptionPane.WARNING_MESSAGE);
            return;
        }
        Drecker.inform("Generating Random Hand for" + tab.getDeck().getId());
        new RandomHandWindow(tab.getDeck()).setVisible(true);
    }

    private void removeSelectedDeck() {
        Deck deck = (Deck) lstDecks.getModel().getElementAt(lstDecks.getSelectedIndex());
        for (int i = 1; i < jTabbedPane.getTabCount(); i++) {
            if (deck.compareTo(((CustomDeckPanel) jTabbedPane.getComponentAt(i)).getDeck()) == 0) {
                jTabbedPane.remove(i);
            }
        }
        Queries.remove(deck);
        loadDecks();
    }

    public void setStatus(String newStatus) {
        lblStatus.setText(newStatus);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelDeck;
    private javax.swing.JButton btnNewDeck;
    private javax.swing.JButton btnOpenDeck;
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSaveAll;
    private javax.swing.JButton btnSimulate;
    private javax.swing.JButton btnStadistics;
    private javax.swing.JButton btnToText;
    private javax.swing.JComboBox cboDemon;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JPopupMenu.Separator jSeparator6;
    private javax.swing.JPopupMenu.Separator jSeparator7;
    private javax.swing.JPopupMenu.Separator jSeparator8;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JLabel lblBackground;
    private javax.swing.JLabel lblBienvenido;
    private javax.swing.JLabel lblComenzar;
    private javax.swing.JLabel lblDemonio;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JList lstDecks;
    private javax.swing.JProgressBar mainjProgressBar;
    private javax.swing.JMenu mnuEdit;
    private javax.swing.JMenuItem mnuF_Exit;
    private javax.swing.JMenuItem mnuF_Export;
    private javax.swing.JMenuItem mnuF_Import;
    private javax.swing.JMenuItem mnuF_New;
    private javax.swing.JMenuItem mnuF_Print;
    private javax.swing.JMenuItem mnuF_Save;
    private javax.swing.JMenuItem mnuF_SaveAll;
    private javax.swing.JMenu mnuFile;
    private javax.swing.JMenuItem mnuH_About;
    private javax.swing.JMenuItem mnuH_Help;
    private javax.swing.JMenu mnuHelp;
    private javax.swing.JPanel statusBarPanel;
    private javax.swing.JLayeredPane welcomePanel;
    // End of variables declaration//GEN-END:variables
}
